//
//  AWStatusesViewModel.swift
//  WeiBo
//
//  Created by AaronWei on 2017/6/8.
//  Copyright © 2017年 AaronWei. All rights reserved.
//

enum CountType {
    
    case reposts
    
    case comments
    
    case attitudes
}

import UIKit

class AWStatusesViewModel: NSObject {
    
    var status: AWStatusesModel? {
        
        didSet {
                        
            guard let currentStatus = status else { return }
            
            handleMbrankImage(mbrank: currentStatus.user?.mbrank ?? 0)
            
            handleVerifiedTypeImage(verified_type: currentStatus.user?.verified_type ?? -1)
            
            handleRetweetContent(status: currentStatus)
            
            handleRepostsOrCommentsOrAttitudesCount(countType: .reposts, count: currentStatus.reposts_count)
            
            handleRepostsOrCommentsOrAttitudesCount(countType: .comments, count: currentStatus.comments_count)
            
            handleRepostsOrCommentsOrAttitudesCount(countType: .attitudes, count: currentStatus.attitudes_count)
            
            handleSourceStr(source: currentStatus.source)
            
        }
        
    }
    
    var mbrankImage: UIImage?
    
    var verifiedTypeImage: UIImage?
    
    var retweetContent: String?
    
    var reposts_count: String?
    
    var comments_count: String?
    
    var attitudes_count: String?
    
    var sourceStr: String?
    
    var timeTextColor: UIColor?
    
    var timeText: String? {
        
        //1.实例化日期格式化对象
        //2.指定格式化符
        sharedDateFormater.dateFormat = "EEE MMM dd HH:mm:ss Z yyyy"
        //在真机上必须设置本地化信息否则转换失败
        sharedDateFormater.locale = Locale(identifier: "en")
        //3.将字符串转换日期对象
        
        timeTextColor = UIColor.lightGray
        
        guard let sinaDate = sharedDateFormater.date(from: status?.created_at ?? "") else {
            return "穿越时空的少年"
        }
        //4.处理日期
        //4.1 判断是否是今年 Calendar 提供非常丰富的日期处理函数
        let calender = Calendar.current
        let currentDate = Date()
        let result = calender.dateComponents([.day,.year], from: sinaDate, to: currentDate)
        if result.year == 0 {
            //今年
            if calender.isDateInToday(sinaDate) {
                //今天
                //获取当前时间和sinaDate秒的差值
                let delta = currentDate.timeIntervalSince(sinaDate)
                if delta < 60 {
                    //刚刚
                    timeTextColor = UIColor.orange
                    return "刚刚"
                } else if delta < 3600 {
                    return "\(Int(delta / 60))分钟前"
                } else {
                    return "\(Int(delta / 3600))小时前"
                }
            } else if calender.isDateInYesterday(sinaDate) {
                //昨天 昨天 HH:mm
                sharedDateFormater.dateFormat = "昨天 HH:mm"
                return sharedDateFormater.string(from: sinaDate)
            } else {
                //其他 MM-dd HH:mm
                sharedDateFormater.dateFormat = "MM-dd HH:mm"
                return sharedDateFormater.string(from: sinaDate)
            }
        } else {
            //非当年
            sharedDateFormater.dateFormat = "yyyy-MM-dd HH:mm"
            return sharedDateFormater.string(from: sinaDate)
        }
    }
    
    private func handleMbrankImage(mbrank: Int) {
        
        if mbrank >= 1 && mbrank <= 6 {
            
            mbrankImage = UIImage(named: "common_icon_membership_level\(mbrank)")
            
        } else {
            
            mbrankImage = #imageLiteral(resourceName: "common_icon_membership_expired")
            
        }
        
    }
    
    private func handleVerifiedTypeImage(verified_type: Int) {
        
        switch verified_type {
        case 0:
            
            verifiedTypeImage = #imageLiteral(resourceName: "avatar_vip")
            
        case 2, 3, 5:
            
            verifiedTypeImage = #imageLiteral(resourceName: "avatar_enterprise_vip")
            
        case 220:
            
            verifiedTypeImage = #imageLiteral(resourceName: "avatar_grassroot")
            
        default:
            verifiedTypeImage = nil
        }
        
    }
    
    private func handleRetweetContent(status: AWStatusesModel) {
        
        if status.retweeted_status != nil {
            
            if let name = status.retweeted_status?.user?.screen_name, let text = status.retweeted_status?.text {
                
                retweetContent = "@\(name): \(text)"
            }
            
        }
        
    }
    
    private func handleRepostsOrCommentsOrAttitudesCount(countType: CountType, count: Int) {
        
        var countStr: String
        
        let num = count / 10000
        
        if num == 0 {
            
            if count <= 0 {
                
                countStr = ""
                
            } else {
                
                countStr = "  \(count)"
                
            }
            
        } else {
            
            countStr = "  \(num)万"
            
        }
        
        switch countType {
            
        case .reposts:
            
            reposts_count = countStr == "" ? "  转发" : countStr
            
        case .comments:
            
            comments_count = countStr == "" ? "  评论" : countStr
            
        case .attitudes:
            
            attitudes_count = countStr == "" ? "  赞" : countStr
            
        }
        
    }
    
    private func handleSourceStr(source: String?) {
        
        if source != "" {
                        
            let sourceStrArray = source?.components(separatedBy: CharacterSet(charactersIn: "<>"))
            
            sourceStr = "来自\(sourceStrArray![2])"
            
        }
        
    }

}
